package com.han.fu.api.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.han.fu.api.business.entity.Follow;
import com.han.fu.api.business.mapper.FollowMapper;
import com.han.fu.api.business.model.ao.FollowAo;
import com.han.fu.api.business.model.ao.FollowPageAo;
import com.han.fu.api.business.model.vo.FollowVo;
import com.han.fu.api.business.service.IFollowService;
import com.han.fu.api.business.service.IUserService;
import com.han.fu.api.common.constants.StringConstants;
import com.han.fu.api.common.model.UserDetail;
import com.han.fu.api.common.utils.CurrentUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 关注接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    IUserService userService;
    @Autowired
    CurrentUserUtil currentUserUtil;

    /**
     * 关注分页查询
     *
     * @param ao 查询条件
     * @return Page<FollowVo> 分页数据
     */
    @Override
    public Page<FollowVo> queryFollowPage(FollowPageAo ao) {
        return baseMapper.queryFollowPage(ao);
    }

    /**
     * 关注新增数据
     *
     * @param ao 数据
     */
    @Override
    public void addFollow(FollowAo ao) {
        UserDetail user = currentUserUtil.getUser();
        if (StringConstants.FOLLOW_OPERATION == ao.getOperation()) {
            Follow entity = new Follow();
            entity.setFollowId(ao.getFollowId());
            entity.setCreateUser(user.getId());
            boolean isExist = SqlHelper.retBool(baseMapper.queryFollowCount(entity));
            if (isExist) {
                log.warn(String.format("用户已关注: %s", ao.getFollowId()));
            } else {
                super.save(entity);
            }
        } else if (StringConstants.CANCEL_OPERATION == ao.getOperation()) {
            Follow entity = new Follow();
            entity.setFollowId(ao.getFollowId());
            entity.setCreateUser(user.getId());
            baseMapper.cancelFollow(entity);
        }
    }

}